<?php


namespace app\api\controller;

use app\BaseController;
use app\service\cxyunService;
use think\facade\Db;

class CxyunNotice extends BaseController
{
    public function alinotice(){
        $path=dir(getcwd())->path.'/cxyun/'.date('Ymd')."/notice";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $info=Db::table("cxyun_order")->where('tradeNo',$result['trade_no'])->find();
            if(!empty($info)){
                if($info["payStatus"]==0){
                    $res=Db::table("cxyun_order")
                        ->where("id",$info["id"])
                        ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['gmt_payment']]);

                    $payNotice["parkingId"]=$info["parkingId"];
                    $payNotice["plateNumber"]=$info["plateNumber"];
                    $payNotice["payFee"]=$info["payFee"];
                    $payNotice["orderNo"]=$info["orderNo"];
                    $payNotice["payType"]=3;
                    $payNotice["timestamp"]=time()."000";
                    $payNotice["orderId"]=$info["tradeNo"];
                    $cxyunApi=new cxyunService();
                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
//                    Db::startTrans();
//                    try {
//                        $res=Db::table("cxyun_order")
//                            ->where("id",$info["id"])
//                            ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['gmt_payment']]);
//
//                        $payNotice["parkingId"]=$info["parkingId"];
//                        $payNotice["plateNumber"]=$info["plateNumber"];
//                        $payNotice["payFee"]=$info["payFee"];
//                        $payNotice["orderNo"]=$info["orderNo"];
//                        $payNotice["payType"]=3;
//                        $payNotice["timestamp"]=time()."000";
//                        $payNotice["orderId"]=$info["tradeNo"];
//                        $cxyunApi=new cxyunService();
//                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
//                        // 提交事务
//                        Db::commit();
//                        return $result;
//                    } catch (\Exception $e) {
//                        // 回滚事务
//                        Db::rollback();
//                        return ;
//                    }


                }
            }
        }
    }

    public function test(){
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunnotice";
        //$path=dir(getcwd())->path.'/pay/20240126/cxyunnotice';
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        //$address = $path."/".date('His').rand(100000,999999).'.txt';
        $address= $path."/"."003035488767.txt";
        //file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        //var_dump($result);
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $info=Db::table("cxyun_order")->where('tradeNo',$result['trade_no'])->find();
            var_dump($info);
            if(!empty($info)){
                if($info["payStatus"]==0){
                    Db::startTrans();
                    try {
                        $res=Db::table("cxyun_order")
                            ->where("id",$info["id"])
                            ->update(["payStatus"=>"1","payType"=>"1","payTime"=>$result['gmt_payment']]);

                        $payNotice["parkingId"]=$info["parkingId"];
                        $payNotice["plateNumber"]=$info["plateNumber"];
                        $payNotice["payFee"]=$info["payFee"];
                        $payNotice["orderNo"]=$info["orderNo"];
                        $payNotice["payType"]=3;
                        $payNotice["timestamp"]=time()."000";
                        $payNotice["orderId"]=$info["tradeNo"];
                        $cxyunApi=new cxyunService();
                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                        var_dump($res);
                        // 提交事务
                        Db::commit();
                        return $result;
                    } catch (\Exception $e) {
                        var_dump($e->getMessage());
                        // 回滚事务
                        Db::rollback();
                        return ;
                    }


                }
            }
        }
    }

}